﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.DirectoryServices;
using System.Web.SessionState;
using System.Text;
using System.Linq;

using LDAP.Entity;
using LDAP.Utility;
using LDAP.Web;
using LDAP.BLL;

namespace LDAP.Handler
{
    public class PublicHandler : BaseWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
            switch (Action) { 
                case "login":
                    Login();
                    break;
                case "logout":
                    Logout();
                    break;
                case "alluser":
                    AllUser();
                    break;

                //case "updateRelationship":
                //    UpdateRelationship();
                //    break;
            }

            context.Response.End();
        }

        #region AiXiaoXin
        //public void UpdateRelationship() {
        //    JsonTransfer jt =new JsonTransfer();
        //    User user = Auth.GetUser();
        //    if (user == null)
        //    {
        //        jt.SetApiFailed("请登录后再试");
        //        currentResponse.Write(jt);
        //        currentResponse.End();
        //        return;
        //    }

        //    Aixiaoxin aixiaoxin = new Aixiaoxin
        //    {
        //        Id = user.Id,
        //        Dept = user.DeptName,
        //        Email = user.Email,
        //        Name = user.UserName,

        //        Angel = currentRequest.Form["angel"] ?? "",
        //        RelationShip1 = currentRequest.Form["rs1"] ?? "",
        //        RelationShip1Id = currentRequest.Form["rs1id"] ?? "",
        //        RelationShip1khD = currentRequest.Form["rs1khd"] ?? "",
        //        RelationShip1khM = currentRequest.Form["rs1khm"] ?? "",
        //        RelationShip1khH = currentRequest.Form["rs1khh"] ?? "",
        //        RelationShip1zH = currentRequest.Form["rs1zh"] ?? ""
        //    };

        //    if (currentRequest.Form["need2"].ToString() == "true") {
        //        aixiaoxin.RelationShip2 = currentRequest.Form["rs2"] ?? "";
        //        aixiaoxin.RelationShip2Id = currentRequest.Form["rs2id"] ?? "";
        //        aixiaoxin.RelationShip2khD = currentRequest.Form["rs2khd"] ?? "";
        //        aixiaoxin.RelationShip2khH = currentRequest.Form["rs2khh"] ?? "";
        //        aixiaoxin.RelationShip2khM = currentRequest.Form["rs2khm"] ?? "";
        //        aixiaoxin.RelationShip2zH = currentRequest.Form["rs2zh"] ?? "";
        //    }

        //    int result = BizAixiaoxin.Add(aixiaoxin);
        //    if (result > 0) {
        //        jt.SetApiSuccess();
        //        currentResponse.Write(jt);
        //        currentResponse.End();
        //    }
        //    jt.SetApiFailed("稍后再试，或者RTX联系下 杨高靖");
        //    currentResponse.Write(jt);
        //    currentResponse.End();
        //}
        #endregion

        public void AllUser() {
            string message = "";
            List<User> list = BizUser.AllUser(ref message);
            if (string.IsNullOrEmpty(message)) {
                Response.Write(JsonTransfer.ToJson(list));
            }
            else
            {
                Response.Write(new JsonTransfer().SetError("Error authenticating user." + message));
            }
        }

        public void Logout() {
            Auth.ClearAuthentication();
            Response.Write(new JsonTransfer().SetApiSuccess());
        }

        public void Login()
        {
            string username = Tools.Web.Request.GetFormString("username").ToUpper();
            string password = Tools.Web.Request.GetFormString("password");

            string message = "";
            User user = BizUser.AllUser(ref message).FirstOrDefault(l => l.Id.ToLower() == username.ToLower());//BizUser.Login(username, password, ref message); //
            if (string.IsNullOrEmpty(message))
            {
                Auth.SetAuthentication(user, 0);
                JsonTransfer jt = new JsonTransfer();
                jt.SetApiSuccess();
                jt.Add("user", user);
                Response.Write(jt);
            }
            else
            {
                Response.Write(new JsonTransfer().SetError("Error authenticating user." + message));
            }
        }
    }
}